#include "Movables.h"

Movables::Movables(Point location, int radius, int speed, Movable_type type, ALLEGRO_BITMAP* bitmap)
	:_location(location),
	_radius(radius), 
	_speed(speed), 
	_direction(RIGHT),	//Set default direction to right
	_type(type),
	_bitmap(bitmap)
{}

void Movables::direction(Direction dir)
{
	_direction = dir;
}

void Movables::location(Point location)
{
	_location = location;
}

Direction Movables::direction()
{
	return _direction;
}

Point Movables::location()
{
	return _location;
}

int Movables::radius()
{
	return _radius;
}

Movable_type Movables::type()
{
	return _type;
}


DetectionResult Movables::checkMoveInDirection(Point location, Direction direction, Map& map)
{
	DetectionResult result = CAN_MOVE;

	int x = location.x();
	int y = location.y();
	int length = 2*RADIUS_OF_MOVABLE + 1;

	int delta_x = delta(direction).at(0);
	int delta_y = delta(direction).at(1);

	for (int i = -RADIUS_OF_MOVABLE; i <= RADIUS_OF_MOVABLE && result == CAN_MOVE; i++)
	{
		int y_check = y + delta_y*(1 + RADIUS_OF_MOVABLE) + delta_x*i;
		int x_check = x + delta_x*(1 + RADIUS_OF_MOVABLE) + delta_y*i;
		Point check_position(x_check, y_check);

		if(x_check < 0 || x_check == GAME_GRID_ELEMENTS || y_check < 0 || y_check == GAME_GRID_ELEMENTS)
			result = COLLISION;
		else
		{
			Movable_type position_type = map.positionType(check_position);
			result = collisionCheck(position_type);
			if(result != CAN_MOVE)
				break;
		}
					
	}
	return result;
}

	ALLEGRO_BITMAP *Movables::bitmap()
	{
		return _bitmap;
	}